package com.butterfly.hotkey.common.result;

import cn.hutool.core.util.PageUtil;
import com.google.common.collect.Lists;

import java.io.Serializable;
import java.util.List;

import static com.butterfly.hotkey.core.constant.NumConst.TEN;

/**
 * @author butterfly
 */
public class PageResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 默认页数 10
     */
    private static final int DEFAULT_PAGE_SIZE = TEN;

    /**
     * 页码,从1开始
     */
    private int page;
    /**
     * 每页结果数
     */
    private int pageSize;
    /**
     * 总页数
     */
    private int totalPage;
    /**
     * 总数
     */
    private int total;

    /**
     * 编码
     */
    private String code;

    /**
     * 编码
     */
    private String msg;

    /**
     * 追踪ID
     */
    private String requestId;

    /**
     * 数据
     */
    private List<T> data;

    /**
     * 构造
     */
    public PageResult() {
        this(0, DEFAULT_PAGE_SIZE);
    }

    /**
     * 构造
     *
     * @param page     页码，1表示第一页
     * @param pageSize 每页结果数
     */
    public PageResult(int page, int pageSize) {
        this.page = Math.max(page, 1);
        this.pageSize = pageSize <= 0 ? DEFAULT_PAGE_SIZE : pageSize;
        this.data = Lists.newArrayList();
    }

    /**
     * 构造
     *
     * @param page     页码，0表示第一页
     * @param pageSize 每页结果数
     * @param total    结果总数
     */
    public PageResult(int page, int pageSize, int total) {
        this(page, pageSize);
        this.total = total;
        this.totalPage = PageUtil.totalPage(total, pageSize);
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }
}
